package com.le.tester.imooc.oldfairy.test;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * createTime：2023/6/3 14:20
 * description：线程的信号唤醒
 */
public class WaitAwait {

    public static void main(String[] args) throws InterruptedException {

        ReentrantLock lock = new ReentrantLock();

        //锁的状态
        Condition condition = lock.newCondition();
        Thread t1 = new Thread(() -> {
            System.out.println("before-wait...1");
            try {
                lock.lock();
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
            System.out.println("after-wait...1");

        });

        Thread t2 = new Thread(() -> {
            System.out.println("before-wait...2");
            try {
                lock.lock();
//                synchronized (obj) {
//                    obj.wait();
//                }
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
            System.out.println("after-wait...2");
        });

        t1.start();
        t2.start();

        Thread.sleep(1000);
        //拿到锁，开始唤醒
        lock.lock();
        condition.signalAll();
        lock.unlock();
    }
}
